package com.ls.fw.office.itext.itext.handler;

import java.io.FileNotFoundException;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfWriter;
import com.ls.fw.office.itext.itext.builder.PdfTOCBuilder;

public class PdfHandler implements IPdfHandler{
    
	private PdfWriter writer = null;
    private IHandler handler = null;
    private final PdfTOCBuilder tocBuilder;
   
	
	public PdfHandler(IHandler handler) throws FileNotFoundException, DocumentException {
	    this.handler = handler;
	    writer = PdfWriter.getInstance(handler.getDocument(), handler.getOut());
	    writer.setLinearPageMode();
		tocBuilder = new PdfTOCBuilder(); 
	    tocBuilder.withRectangle(handler.getDocument().getPageSize()).withOriginalBuilder(this);
	    this.handler.open();
	}
	
 
	public Document getDocument(){
		return handler.getDocument();
	}

	@Override
	public PdfWriter getWriter() {
		return writer;
	}

	public IHandler getHandler() {
		return handler;
	}

	public void setHandler(IHandler handler) {
		this.handler = handler;
	}

	@Override
	public void close() {
		this.handler.close();
		if(this.writer!=null){
			this.writer.close();
		}
	}

}
